var CallRecordTotalTable = {
    id: "CallRecordTotalTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
CallRecordTotalTable.initColumn = function () {
    var columns = [
        {
            field: '', title: '', visible: false,
            formatter: function (value, row, index) {
                return index + 1
            }
        },
        {title: 'customerSexHead', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {
            title: '人员', field: 'name', valign: 'middle', align: 'center', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='color: green;display: flex;align-items: center;'<img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            }
        },
        {
            title: '累计时长', field: 'totalHoldS', align: 'center', valign: 'middle', sortable: true, width: 140,
            formatter: function (value, row, index) {
                return secondToTimeStr(value);
            },
        },
        {title: '累计个数', field: 'totalNumber', align: 'center', valign: 'middle', sortable: true, width: 110,},
        {
            title: '平均时长', field: '', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (row.totalNumber === 0) {
                    return 0;
                } else {
                    var timeline = Math.floor(row.totalHoldS / row.totalNumber)
                    return secondToTimeStr(timeline);
                }
            },
        },
        {title: '有效个数', field: 'effectiveNumber', align: 'center', valign: 'middle', sortable: true, width: 110,},
        {title: '无效个数', field: 'invalidNumber', align: 'center', valign: 'middle', sortable: true, width: 170,},
        {title: '到店个数', field: 'arrivalNumber', align: 'center', valign: 'middle', sortable: true, width: 90,},
    ];
    return columns;
};

CallRecordTotalTable.search = function () { // 查询预约客户列表
    CallRecordTotalTable.table.refresh({query: CallRecordTotalTable.formParams()});
};

CallRecordTotalTable.close = function () { // 关闭窗口
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};
CallRecordTotalTable.selectionTwo = function (text, obj) {
    document.getElementById("area").value = $("#area").val();
    $.ajax({
        url: Feng.ctxPath + "/reservationsCallRecord/getWanName",
        type: "post",
        data: {
            area: document.getElementById("area").value
        },
        success: function (data) {
            var txt = '';
            for (var i = 0; i < data.length; i++) {
                txt += ' <option value="' + data[i].account + '"> ' + ' &nbsp' + data[i].account + '</option>';
            }
            document.getElementById('wanName').innerHTML = txt;
            CallRecordTotalTable.search();
        },
        error: function (data) {
            alert('没有权限，请联系管理员!');
        }
    });
};

CallRecordTotalTable.formParams = function () { // 市场预约表单查询提交参数
    var queryData = {};
    queryData['area'] = $("#area").val();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    queryData['areaAbbreviation'] = $("#areaAbbreviation", parent.document).val();
    queryData['startCreateTime'] = $("#startCreateTime").val();  //建档时间
    queryData['endCreateTime'] = $("#endCreateTime").val();
    return queryData;
};

function secondToTimeStr(t) {
    if (!t) return;
    if (t < 60) return "" + ((i = t) < 10 ? "0" + i + "秒" : i + "秒");
    if (t < 3600) return "" + ((a = parseInt(t / 60)) < 10 ? "0" + a + "分" : a + "分") + ((i = t % 60) < 10 ? "0" + i + "秒" : i + "秒");
    if (3600 <= t) {
        var a, i, e = parseInt(t / 3600);
        return (e < 10 ? "0" + e + "时" : e + "时") + ((a = parseInt(t % 3600 / 60)) < 10 ? "0" + a + "分" : a + "分") + ((i = t % 60) < 10 ? "0" + i + "秒" : i + "秒");
    }
}

CallRecordTotalTable.today = function () { // 点击上面状态筛选表格
    $("#startCreateTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
    $("#endCreateTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
    CallRecordTotalTable.search();
};

CallRecordTotalTable.yesterday = function () { // 昨天
    $("#startCreateTime").val(setDateTime(new Date(), 1) + " " + "00:00:00");
    $("#endCreateTime").val(setDateTime(new Date(), 1) + " " + "23:59:59");
    CallRecordTotalTable.search();
};

CallRecordTotalTable.thisWeek = function () { // 本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startCreateTime").val(setDateTime(monday, 0) + " " + "00:00:00");
    $("#endCreateTime").val(setDateTime(sunday, 0) + " " + "23:59:59");
    ReservationsRecordTable.search();
};

CallRecordTotalTable.thisMonth = function () { // 本月
    $("#startCreateTime").val(firstMonthTime(new Date(), 0) + " " + "00:00:00");
    $("#endCreateTime").val(setMonthTime(new Date(), 0) + " " + "23:59:59");
    CallRecordTotalTable.search();
};

CallRecordTotalTable.lastMonth = function () { // 上个月
    $("#startCreateTime").val(firstMonthTime(new Date(), 1) + " " + "00:00:00");
    $("#endCreateTime").val(setMonthTime(new Date(), 1) + " " + "23:59:59");
    CallRecordTotalTable.search();
}

CallRecordTotalTable.resetSearch = function () { // 重置
    $("#marketInputPublic", parent.document).val("");
    $("#startCreateTime").val("");
    $("#endCreateTime").val("");
    CallRecordTotalTable.search();
};

$(function () {
    var seleteStoreValue = $('#area').val();
    if (seleteStoreValue != "") {
        $("#area option[value='" + seleteStoreValue + "']").attr("selected", "selected")
    }
    var defaultColunms = CallRecordTotalTable.initColumn();
    var table = new BSTable(CallRecordTotalTable.id, "/reservationsCallRecord/callTotalList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CallRecordTotalTable.formParams());
    CallRecordTotalTable.table = table.init();
    CallRecordTotalTable.today();
    menuNameHideColumn($("#menuName").val(), 'CallRecordTotalTable');
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {
        CallRecordTotalTable.search();
    });
    checkDate = function () {
        CallRecordTotalTable.search();
    };
    callbackTable = function () {
        CallRecordTotalTable.search();
    };
});